package com.hoshiicloud.user.controller.rbac;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoshiicloud.UsersLoginVO;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.entity.company.ComUsers;
import com.hoshiicloud.user.entity.company.Company;
import com.hoshiicloud.user.service.company.ICompanyService;
import com.hoshiicloud.user.service.rbac.IComUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import java.util.List;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 公司用户基本信息表 前端控制器
 * </p>
 * 2019-04-24 WGJ
 */
@RestController
@RequestMapping("/rbac/com-users")
@Api(value = "公司用户基本管理", tags = {"公司用户基本操作接口"})
@ApiSort(9)
public class ComUsersController {

    private final IComUsersService comUsersService;

    private final ICompanyService companyService;

    public ComUsersController(IComUsersService comUsersService, ICompanyService companyService) {
        this.comUsersService = comUsersService;
        this.companyService = companyService;
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "查询",
            notes = "查询公司用户基本信息",
            produces = "application/json",
            consumes = "application/json")
    public Response findComUserById(@PathVariable("id") Long id) {
        return Response.data(comUsersService.getById(id));
    }


    @GetMapping("/find/{name}")
    @ApiOperation(value = "查询",
            notes = "根据名称查询公司用户基本信息",
            produces = "application/json",
            consumes = "application/json")
    public Response findComUserByName(@PathVariable("name") String name) {
        QueryWrapper<ComUsers> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ComUsers::getRealName, name);
        return Response.data(comUsersService.getOne(wrapper));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增",
            notes = "新增公司用户基本信息",
            produces = "application/json",
            consumes = "application/json")
    public Response<Long> saveComUser(@RequestBody ComUsers entity) {
        return Response.data(comUsersService.saveComUsers(entity));
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改",
            notes = "修改公司用户基本信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response updateComUserById(@RequestBody ComUsers entity) {
        return Response.data(comUsersService.updateById(entity));
    }

    @ApiOperation(value = "根据用户名和密码获取系统用户信息", notes = "返回值包括用户的角色与权限")
    @PostMapping("/getLoginDetail")
    public Response<UsersLoginVO> getLoginDetail(@RequestBody String phone) {
        return Response.data(comUsersService.getComUserLoginDetail(phone));
    }


    @ApiOperation(value = "根据用户名和密码获取系统用户信息", notes = "返回值包括用户的角色与权限")
    @PostMapping("/getComUserLoginDetail")
    public Response<TokenUsersVO> getCompanyUserLoginDetail(@RequestBody Map<String, String> map) {
        return Response.data(comUsersService.getCompanyUserLoginDetail(map));
    }

    @GetMapping("getCompanyUserList")
    public Response<List<ComUsers>> getCompanyUserList(@RequestParam("privateDomain") String privateDomain,
            @RequestParam("userName") String userName,
            @RequestParam("password") String password) {
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        companyQueryWrapper.lambda()
                .eq(Company::getPrivateDomain, privateDomain);
        Company company = companyService.getOne(companyQueryWrapper);
        QueryWrapper<ComUsers> comUsersQueryWrapper = new QueryWrapper<>();
        comUsersQueryWrapper.lambda().eq(ComUsers::getCompanyId, company.getId())
                .eq(ComUsers::getPhone, userName)
                .eq(ComUsers::getPassword, password);
        return Response.data(comUsersService.list(comUsersQueryWrapper));
    }

    @PostMapping("getCompanyUserListByModel")
    public Response<ComUsers> getCompanyUserListByModel(@RequestBody ComUsers comUsers) {
        QueryWrapper<ComUsers> comUsersQueryWrapper = new QueryWrapper<>();
        comUsersQueryWrapper.lambda().eq(ComUsers::getId, comUsers.getId())
                .eq(ComUsers::getCompanyId, comUsers.getCompanyId());
        return Response.data(comUsersService.getOne(comUsersQueryWrapper));
    }
}
